<template>
  <div>
    <!--隐藏的修改，添加页面-->
    <el-dialog title="社区互动详情" :visible.sync="dialogFormVisible">
      <el-card style="width: 600px;margin-left: 15%">
        <!--左边的div-->
        <div style="height: 600px;width: 20%">
          <!--用户头像-->
          <div class="block" style="margin-left: 40%">
            <el-avatar shape="square" :size="60"/>
          </div>
        </div>
        <!--右边的div-->
        <div style="height: 500px;width: 70%;float: right;margin-top: -107%;margin-right: 10%">
          <div>
            <!--右边（上面）nickname-->
            <div style="margin-top: -2%;margin-left: 2%">
              <!-- 用户昵称 -->
              <p class="nickname" style="font-size:22px;color: #001528">{{ interactionVO.ownerNickname }}</p>
            </div>
            <!--右边（中间一）connet-->
            <div style="margin-top: 5%;margin-left: 1%;margin-bottom: 2%">
              <!-- 帖子内容 -->
              <div class="post-content" style="font-size: 20px;font-family: 'Arial Rounded MT Bold'">
                {{ interactionVO.content }}
              </div>
            </div>
            <!--右边（中间二）图片-->
            <div class="demo-image">
              <!--帖子的图片-->
              <div class="block">
                <el-image
                  style="width: 200px; height: 200px;margin-top: 2%;margin-bottom: 2%"
                  :src="url"
                  :fit="fit"
                />
              </div>
              <!-- 帖子的发表时间 -->
              <div class="time" style="font-size: 18px;color: #bfcbd9;margin-bottom: 2%">{{ interactionVO.createTime }}</div>
            </div>
            <!--右边（下面）评论-->
            <div>
              <el-card class="box-card" style="width: 100%;background-color: #f3f3f3" shadow="never">
                <el-empty
                  v-for="(item,index) in commentVOs"
                  :key="index"
                >
                    <!-- 评论作者 -->
                    <div class="comment-nickname one-line" style="font-size: 20px;color: lightslategrey">
                      {{ item.ownerNickname }}:
                    </div>
                    <!-- 评论内容 -->
                    <div style="font-size: 20px">
                      {{ item.content }}
                      <span
                        style="font-size: 12px"
                        @click="deleteComment(item)"
                      ><i class="el-icon-delete">删除</i></span>
                    </div>
                  </el-empty>
                <el-empty description="描述文字"></el-empty>
              </el-card>
            </div>
          </div>
        </div>

      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <!--主要的表单-->
    <el-form ref="interactionVO" :inline="true" :model="interaction" class="interactionVO-form-inline">
      <el-form-item label="业主昵称:">
        <el-input
          v-model="interaction.ownerNickname"
          placeholder="请输入业主昵称"
          prefix-icon="el-icon-search"
        />
      </el-form-item>
      <el-form-item label="业主姓名：">
        <el-input
          v-model="interaction.ownerRealName"
          placeholder="请输入业主姓名"
          prefix-icon="el-icon-search"
        />
      </el-form-item>
      <el-form-item label="电话号码：">
        <el-input
          v-model.number="interaction.ownerPhoneNumber"
          placeholder="请输入电话号码"
          prefix-icon="el-icon-search"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadInteraction(current, size, interaction)">查询</el-button>
        <el-button @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="interactionVOs"
      style="width: 100% ; box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1) ;margin-top: 1%;margin-left: 2%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="30"
      />
      <el-table-column
        label="序号"
        align="center"
      >
        <template slot-scope="scope">
          <span>{{ scope.$index +1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="业主昵称"
        align="center"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.ownerNickname }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="业主名字"
        align="center"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.ownerRealName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="手机号码"
        align="center"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.ownerPhoneNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="发布时间"
        align="center"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="内容"
        width="200"
        align="center"
      >
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.content }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="图片"
        align="center"
        width="300"
      >
        <template>
          <div class="block">
            <span class="demonstration" />
            <el-carousel trigger="click" height="110px">
              <el-carousel-item v-for="item in 3" :key="item" />
            </el-carousel>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
        align="center"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row),dialogFormVisible = true"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页标签-->
    <div class="el-pagination">
      <el-pagination
        background
        :current-page="this.current"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="this.size"
        layout=" sizes, prev, pager, next, jumper"
        :total="this.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>

export default {
  name: 'Interaction',
  data() {
    return {
      interactionVO: { ownerNickname: '', ownerRealName: '', ownerPhoneNumber: '', interactionId: '', createTime: '', content: '' },
      interactionVOs: [], // 所有页面的数据
      commentVOs: [], // 存放前端传来的评论有关数据
      commentVO: { ownerNickname: '', ownerRealName: '', createTime: '', comment: '', interactionId: '', commentId: '' },
      interaction: { ownerNickname: '', ownerRealName: '', ownerPhoneNumber: '' },
      dialogFormVisible: false,
      formLabelWidth: '120px',
      size: 3,
      current: 1,
      total: 0,
      url: 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
    }
  },
  created() {
    this.loadInteraction(this.current, this.size)
  },
  // 事件调用的方法
  methods: {
    loadInteraction(current, size, interaction) {
      this.axios({
        url: '/interactions?' + this.qs.stringify(interaction),
        methods: 'GET',
        params: {
          current: current,
          size: size
        }
      })
        .then(res => {
          this.interactionVOs = res.data.data.records
          this.current = res.data.data.current
          this.size = res.data.data.size
          this.total = res.data.data.total
        })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 重置按钮
    resetForm() {
      this.interactionVO = { ownerNickname: '', ownerRealName: '', ownerPhoneNumber: '', interactionId: '', createTime: '', content: '' }
      this.loadInteraction(this.current, this.size)
    },
    handleEdit(index, row) {
      this.axios.get('/interaction/' + row.interactionId)
        .then(res => {
          this.interactionVO = row
          this.commentVOs = res.data.data
        })
    },
    // 删除内容以及评论
    handleDelete(index, row) {
      this.$confirm('这项操作将删除该业主发布的内容以及所有相关评论, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.delete('/interactions/' + row.interactionId)
          .then(res => {
            this.loadInteraction(this.current, this.size)
          })
        this.$notify({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$notify({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 删除评论
    deleteComment(item) {
      this.$confirm('这项操作将删除该业主发布的内容以及所有相关评论, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.axios.delete('/interaction/' + item.commentId)
          .then(res => {
            // eslint-disable-next-line eqeqeq
            this.$notify({
              type: 'success',
              message: '删除成功!'
            })
            this.axios.get('/interaction/' + item.interactionId)
              .then(res => {
                this.commentVOs = res.data.data
              })
          })
      })
    },
    // 分页插件
    handleSizeChange(val) {
      this.size = val
      this.loadInteraction(this.current, this.size, this.interaction)
    },
    handleCurrentChange(val) {
      this.current = val
      this.loadInteraction(this.current, this.size, this.interaction)
    }
  }
}
</script>

<style scoped>
  .interactionVO-form-inline
  {
    margin-left: 2%;
    margin-top: 2%;
  }
  .el-carousel__item{
    background-color: #97a8be;
  }
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    margin: 0;
  }
  .el-pagination{
    margin-left: 1%;
    margin-top: 1%
  }
</style>
